/****************************************************************************
 Copyright (c) 2014      PlayFirst Inc.
 Copyright (c) 2014-2016 Chukong Technologies Inc.
 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
 Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).

 https://axmol.dev/

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 ****************************************************************************/

#ifndef __AX_REF_PTR_H__
#define __AX_REF_PTR_H__
/// @cond DO_NOT_SHOW

#include "base/Object.h"
#include "base/Macros.h"
#include <functional>
#include <type_traits>

NS_AX_BEGIN

/**
 * Utility/support macros. Defined to enable RefPtr<T> to contain types like 'const T' because we do not
 * regard retain()/release() as affecting mutability of state.
 */
#define AX_REF_PTR_SAFE_RETAIN(ptr)                                   \
                                                                      \
    do                                                                \
    {                                                                 \
        if (ptr)                                                      \
        {                                                             \
            (ptr)->retain(); \
        }                                                             \
                                                                      \
    } while (0);

#define AX_REF_PTR_SAFE_RELEASE(ptr)                                   \
                                                                       \
    do                                                                 \
    {                                                                  \
        if (ptr)                                                       \
        {                                                              \
            (ptr)->release(); \
        }                                                              \
                                                                       \
    } while (0);

#define AX_REF_PTR_SAFE_RELEASE_NULL(ptr)                              \
                                                                       \
    do                                                                 \
    {                                                                  \
        if (ptr)                                                       \
        {                                                              \
            (ptr)->release(); \
            ptr = nullptr;                                             \
        }                                                              \
                                                                       \
    } while (0);

// Tell RefPtr, the object already referenced, no need to retain again
template <typename T>
struct ReferencedObject
{
    T* _ptr;
};

/**
 * Wrapper class which maintains a strong reference to a axmol ax::Object* type object.
 * Similar in concept to a boost smart pointer.
 *
 * Enables the use of the RAII idiom with axmol objects and helps automate some of the more
 * mundane tasks of pointer initialization and cleanup.
 *
 * The class itself is modelled on C++ 11 std::shared_ptr, and trys to keep some of the methods
 * and functionality consistent with std::shared_ptr.
 */
template <typename T>
class RefPtr
{
public:
    RefPtr() : _ptr(nullptr) {}

    RefPtr(RefPtr<T>&& other)
    {
        _ptr       = other._ptr;
        other._ptr = nullptr;
    }

    RefPtr(T* ptr) : _ptr(ptr) { AX_REF_PTR_SAFE_RETAIN(_ptr); }

    template <typename _Other>
    RefPtr(ReferencedObject<_Other>&& ptr) : _ptr(ptr._ptr)
    {}

    RefPtr(std::nullptr_t ptr) : _ptr(nullptr) {}

    RefPtr(const RefPtr<T>& other) : _ptr(other._ptr) { AX_REF_PTR_SAFE_RETAIN(_ptr); }

    ~RefPtr() { AX_REF_PTR_SAFE_RELEASE_NULL(_ptr); }

    RefPtr<T>& operator=(const RefPtr<T>& other)
    {
        if (other._ptr != _ptr)
        {
            AX_REF_PTR_SAFE_RELEASE(_ptr);
            _ptr = other._ptr;
            AX_REF_PTR_SAFE_RETAIN(_ptr);
        }

        return *this;
    }

    RefPtr<T>& operator=(RefPtr<T>&& other)
    {
        if (&other != this)
        {
            AX_REF_PTR_SAFE_RELEASE(_ptr);
            _ptr       = other._ptr;
            other._ptr = nullptr;
        }

        return *this;
    }

    RefPtr<T>& operator=(T* other)
    {
        if (other != _ptr)
        {
            AX_REF_PTR_SAFE_RETAIN(other);
            AX_REF_PTR_SAFE_RELEASE(_ptr);
            _ptr = other;
        }

        return *this;
    }

    RefPtr<T>& operator=(std::nullptr_t other)
    {
        AX_REF_PTR_SAFE_RELEASE_NULL(_ptr);
        return *this;
    }

    operator T*() const { return _ptr; }

    T& operator*() const
    {
        AXASSERT(_ptr, "Attempt to dereference a null pointer!");
        return *_ptr;
    }

    T* operator->() const
    {
        AXASSERT(_ptr, "Attempt to dereference a null pointer!");
        return _ptr;
    }

    T* get() const { return _ptr; }

    bool operator==(const RefPtr<T>& other) const { return _ptr == other._ptr; }

    bool operator==(const T* other) const { return _ptr == other; }

    bool operator==(typename std::remove_const<T>::type* other) const { return _ptr == other; }

    bool operator==(const std::nullptr_t other) const { return _ptr == other; }

    bool operator!=(const RefPtr<T>& other) const { return _ptr != other._ptr; }

    bool operator!=(const T* other) const { return _ptr != other; }

    bool operator!=(typename std::remove_const<T>::type* other) const { return _ptr != other; }

    bool operator!=(const std::nullptr_t other) const { return _ptr != other; }

    bool operator>(const RefPtr<T>& other) const { return _ptr > other._ptr; }

    bool operator>(const T* other) const { return _ptr > other; }

    bool operator>(typename std::remove_const<T>::type* other) const { return _ptr > other; }

    bool operator<(const RefPtr<T>& other) const { return _ptr < other._ptr; }

    bool operator<(const T* other) const { return _ptr < other; }

    bool operator<(typename std::remove_const<T>::type* other) const { return _ptr < other; }

    bool operator>=(const RefPtr<T>& other) const { return _ptr >= other._ptr; }

    bool operator>=(const T* other) const { return _ptr >= other; }

    bool operator>=(typename std::remove_const<T>::type* other) const { return _ptr >= other; }

    bool operator<=(const RefPtr<T>& other) const { return _ptr <= other._ptr; }

    bool operator<=(const T* other) const { return _ptr <= other; }

    bool operator<=(typename std::remove_const<T>::type* other) const { return _ptr <= other; }

    explicit operator bool() const { return _ptr != nullptr; }

    void reset() { AX_REF_PTR_SAFE_RELEASE_NULL(_ptr); }

    void swap(RefPtr<T>& other)
    {
        if (&other != this)
        {
            T* tmp     = _ptr;
            _ptr       = other._ptr;
            other._ptr = tmp;
        }
    }

    /**
     * This function assigns to this RefPtr<T> but does not increase the reference count of the object pointed to.
     * Useful for assigning an object created through the 'new' operator to a RefPtr<T>. Basically used in scenarios
     * where the RefPtr<T> has the initial ownership of the object.
     *
     * E.G:
     *      RefPtr<ax::Image> image;
     *      image.weakAssign(new ax::Image());
     *
     * Instead of:
     *      RefPtr<ax::Image> image;
     *      image = new ax::Image();
     *      image->release();               // Required because new'd object already has a reference count of '1'.
     */
    void weakAssign(const RefPtr<T>& other)
    {
        AX_REF_PTR_SAFE_RELEASE(_ptr);
        _ptr = other._ptr;
    }

private:
    T* _ptr;

    // NOTE: We can ensure T is derived from ax::Object at compile time here.
    static_assert(axstd::is_ref_counted_v<typename std::remove_const<T>::type>, "T must be derived from Object");
};

template <class T>
inline RefPtr<T> makeRef(T* ptr)
{
    return RefPtr<T>(ptr);
}

template <class T>
inline bool operator<(const RefPtr<T>& r, std::nullptr_t)
{
    return std::less<T*>()(r.get(), nullptr);
}

template <class T>
inline bool operator<(std::nullptr_t, const RefPtr<T>& r)
{
    return std::less<T*>()(nullptr, r.get());
}

template <class T>
inline bool operator>(const RefPtr<T>& r, std::nullptr_t)
{
    return nullptr < r;
}

template <class T>
inline bool operator>(std::nullptr_t, const RefPtr<T>& r)
{
    return r < nullptr;
}

template <class T>
inline bool operator<=(const RefPtr<T>& r, std::nullptr_t)
{
    return !(nullptr < r);
}

template <class T>
inline bool operator<=(std::nullptr_t, const RefPtr<T>& r)
{
    return !(r < nullptr);
}

template <class T>
inline bool operator>=(const RefPtr<T>& r, std::nullptr_t)
{
    return !(r < nullptr);
}

template <class T>
inline bool operator>=(std::nullptr_t, const RefPtr<T>& r)
{
    return !(nullptr < r);
}

/**
 * Cast between RefPtr types statically.
 */
template <class T, class U>
RefPtr<T> static_pointer_cast(const RefPtr<U>& r)
{
    return RefPtr<T>(static_cast<T*>(r.get()));
}

/**
 * Cast between RefPtr types dynamically.
 */
template <class T, class U>
RefPtr<T> dynamic_pointer_cast(const RefPtr<U>& r)
{
    return RefPtr<T>(dynamic_cast<T*>(r.get()));
}

/**
 * Done with these macros.
 */
#undef AX_REF_PTR_SAFE_RETAIN
#undef AX_REF_PTR_SAFE_RELEASE
#undef AX_REF_PTR_SAFE_RELEASE_NULL

NS_AX_END

/// @endcond
#endif  // __AX_REF_PTR_H__
